package com.epcs.model.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.epcs.model.dto.Account;
import com.epcs.model.dto.Email;
import com.epcs.model.dto.Following;
import com.epcs.model.dto.Role;
import com.epcs.util.DBUtil;

public class UserDAO {

	public Account getUser(String username, String password) {

		Session session = DBUtil.getSession();
		try {
			Criteria criteria = session.createCriteria(Account.class);
			criteria.add(Restrictions.eq("username", username)).add(Restrictions.eq("password", password));
			Account account = (Account) criteria.uniqueResult();
			if (account != null) {
				return account;
			}
		} finally {
			
			if (session != null) {
				session.close();
			}
		}

		return null;
	}
	
	public Account loadUser(String username) {

		Session session = DBUtil.getSession();
		try {
			Criteria criteria = session.createCriteria(Account.class);
			criteria.add(Restrictions.eq("username", username));
			Account account = (Account) criteria.uniqueResult();
			if (account != null) {
				return account;
			}
		} finally {
			if (session != null) {
				session.close();
			}
		}

		return null;
	}

	public String[] loadUserByKeyword(String keyword, List<String> usernameList) {

		Session session = DBUtil.getSession();
		
		try {
			Criteria criteria = session.createCriteria(Account.class);
			criteria.add(Restrictions.like("username", "%" + keyword + "%"));
			criteria.add(Restrictions.not(Restrictions.in("username", usernameList)));

			List<Account> list = criteria.list();
			String[] resultList = new String[list.size()];
			if (list != null) {
				for (int i = 0; i< list.size(); i++){
					Account account  = list.get(i);
					
					resultList[i] = account.getUsername();
				}
				
				return resultList;
			}
		} finally {
			if (session != null) {
				session.close();
			}
		}

		return null;
	}

	public boolean createAccount(Account account) {

		Session session = DBUtil.getSession();
		Transaction trans = null;
		try {
			trans = session.beginTransaction();
			session.save(account);
			trans.commit();
			return true;
		} catch (HibernateException e) {
			System.out.println(e.getMessage());
			trans.rollback();
		} 
		finally {
			if (session != null) {
				session.close();
			}
		}

		return false;
	}

	public boolean updateAccount(Account account) {

		Session session = DBUtil.getSession();
		Transaction trans = null;
		try {
			trans = session.beginTransaction();
			session.update(account);
			trans.commit();
			return true;
		} catch (HibernateException e) {
			System.out.println(e.getMessage());
			trans.rollback();
		} 
		finally {
			if (session != null) {
				session.close();
			}
		}

		return false;
	}
	public List<Account> getAllAccount() {
		Session session = null;
		try {	
			Role role = new Role();
			role.setRoleId(1);
			role.setRoleName("Admin");
			session = DBUtil.getSession();
			Criteria criteria = session.createCriteria(Account.class);
			criteria.add(Restrictions.not(Restrictions.eq("role", role)));
			List<Account> listAccount = new ArrayList<Account>();
			listAccount = criteria.list();
			if (listAccount.size() > 0){
				return listAccount;
			} 
			
		} catch (HibernateException e) {
			System.out.println("Rolback: \n" + e.getMessage());
			
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return null;
	}
}
